import { Component, OnInit , ViewChild} from '@angular/core';
import { CommonService } from '../../service/common.service'
import { Router} from '@angular/router'; //导入router服务
import { EventService } from '../../service/event.service'
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-wallet',
  templateUrl: './wallet.page.html',
  styleUrls: ['./wallet.page.scss'],
})
export class WalletPage implements OnInit {
  
  @ViewChild('copy') copy;
  @ViewChild('copy2') copy2;

  public ispwd:boolean = false

  public isfil:string = ""

  public isxch:string = ""

  public userInfo:any;
  public password:string;
  public id:number;
  constructor(private common:CommonService,private router:Router,
    private eventService:EventService,private alertController:AlertController) { }

  ngOnInit() {
    this.userInfo = this.common.getInfo()
    this.isqb()
    this.isqba()
  }
  
  // 获取钱包信息
  qbinfo(name){
    var api = '/tbr/wallet/listData'
    this.common.get(api).then((res:any)=>{
      console.log(res)
      if(res.code == '200'){
        for(var i=0;i<res.data.length;i++){
          if(res.data[i].coinType == name){
            this.id = res.data[i].id
            console.log(this.id)
          }
        }
        // this.common.presentToast(res.msg)
      } 
    })
  }
  // 解绑
  onjb(){
    // this.expressionB = true
    this.presentAlertUrl("解绑后该钱包从管理列表删除")
    this.qbinfo("fil")
  } 
  onjb2(){
    this.presentAlertUrl("解绑后该钱包从管理列表删除")
    this.qbinfo("xch")
  }
  confirmB(){
    var api = '/tbr/wallet/deleteWallet'
    var params = {
      "paymentCode" : this.password, 
      "id": this.id
    }
    
    this.common.post(api,params,false).then((res:any)=>{
      console.log(res)
      if(res.code == '200'){
        this.password = ''
        this.ispwd = false
        this.isqb()
        this.isqba()
        this.common.presentToast(res.msg)
      } 
    })
  }

  event(){
    // 通知用户中心更新用户信息
    this.eventService.event.emit('userstate');
    this.eventService.event.emit('useraction');
    console.log(this.eventService)
  } 

  // 复制文本
  onCopy(){
    this.copy.nativeElement.select()     
    this.common.Clipboard.copy(this.copy.nativeElement.value)
  }

  onCopy2(){
    this.copy2.nativeElement.select()     
    this.common.Clipboard.copy(this.copy2.nativeElement.value)
  }


  // 判断是否绑定钱包
  isqb(){
    var api = '/tbr/wallet/validWallet?coinType=fil'
    this.common.get(api).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
          this.isfil = res.data.fil
      }
    })
  }

  isqba(){
    var api = '/tbr/wallet/validWallet?coinType=xch'
    this.common.get(api).then((res:any)=>{
      console.log(res)
      if(res.code == 200){
          this.isxch = res.data.xch
      }
    })
  }

  // 弹出框
  async presentAlertUrl(message:string) {
    const alert = await this.alertController.create({
        // cssClass: 'my-custom-class',
        header: '提示',
        // subHeader: 'Subtitle',
        message: message,
        buttons: [
          {
            text: '取消',
            role: 'cancel'
          },
          {
            text: '确定',
            handler: (blah) => {
              this.ispwd = true
              // this.type = 'fil'
              // console.log(blah)
            }
          }
        ]
    });
    await alert.present();
  }




}
